﻿using AutoMapper;
using Dora.Orm.SqlSugarCore.UnitOfWork;
using Dora.Services.AdminUser.Application.Base;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Dto.ViewDto;
using Dora.Services.AdminUser.Entities;
using Dora.Services.AdminUser.IApplication;
using Dora.Services.AdminUser.Repository.Repositorys;
using Dora.Tools.Enums.AdminUser;
using Dora.Tools.Utility.Const;
using Microsoft.Extensions.Logging;
using Surging.Core.Common;
using Surging.Core.CPlatform.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Dora.Services.AdminUser.Application
{
    /// <summary>
    /// 环信用户联系表服务
    /// </summary>
    [ModuleName(DoraModuleName.AdminUser)]
    public class HxUserContactService : DoraServiceBase, IHxUserContactService
    {
        
        private ISugarUnitOfWork _unitOfWork;
        private readonly HxUserContactRepository _repository;
        public HxUserContactService(ISugarUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _repository = new HxUserContactRepository(unitOfWork);
        }


        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultDto<Guid>> Create(HxUserContactEntity entity)
        {
            entity.CreatedTime = DateTime.Now;
            var result = await _repository.InsertAsync(entity);
            return ResultDto<Guid>.Success(result.Id);
        }


        /// <summary>
        /// 根据Id获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<HxUserContactEntity> GetById(Guid id)
        {
            return await _repository.FirstOrDefaultAsync(id);
        }


        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<HxUserContactEntity>> GetPageList(HxUserContactQueryDto input)
        {
            var page = await _repository.GetPageList(input);
            return page;
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> Update(HxUserContactEntity entity)
        {
            return await _repository.UpdateAsync(entity);
        }

        /// <summary>
        /// 根据IdList获取列表
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public async Task<List<HxUserContactEntity>> GetList(List<Guid> idList)
        {
            if (idList == null || idList.Count == 0)
            {
                return new List<HxUserContactEntity>();
            }
            var list = await _repository.GetAllListAsync(it => idList.Contains(it.Id));
            return list;
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public async Task<List<HxUserContactEntity>> GetAll()
        {
            var list = await _repository.GetAllListAsync();
            return list;
        }

        /// <summary>
        /// 根据发送人或接收人IdList获取列表
        /// </summary>
        /// <param name="userIdList"></param>
        /// <returns></returns>
        public async Task<List<HxUserContactEntity>> GetListByUserIdList(List<Guid> userIdList)
        {
            if (userIdList == null || userIdList.Count == 0)
            {
                return new List<HxUserContactEntity>();
            }
            var list = await _repository.GetAllListAsync(it => userIdList.Contains(it.SelfUserId)||userIdList.Contains(it.ContactUserId));
            return list;
        }
    }
}
